#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "appdata.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)

#ifdef HILDON
void create_window(HildonWindow* window1)
#else
void create_window(GtkWidget* window1)
#endif
{
  GtkWidget *vbox1;
#ifdef HILDON
  GtkMenu *menubar1;
#else
  GtkWidget *menubar1;
#endif
  GtkWidget *menuitem1;
  GtkWidget *menuitem1_menu;
  GtkWidget *new1;
  GtkWidget *open1;
  GtkWidget *save1;
  GtkWidget *save_as1;
  GtkWidget *separatormenuitem1;
  GtkWidget *quit1;
  GtkWidget *menuitem3;
  GtkWidget *menuitem3_menu;
  GSList *knots1_group = NULL;
  GtkWidget *separator1;
  GtkWidget *zoom_in1;
  GtkWidget *zoom_out1;
  GtkWidget *separator2;
  GtkWidget *toolbar1;
  GtkWidget *menuitem4;
  GtkWidget *menuitem4_menu;
  GtkWidget *help_on_knot1;
  GtkWidget *about1;
  GtkIconSize tmp_toolbar_icon_size;
  GtkWidget *toolbutton1;
  GtkWidget *toolbutton2;
  GtkWidget *toolbutton3;
  GtkWidget *toolbutton4;
  GtkWidget *separatortoolitem1;
  GSList *radiotoolbutton1_group = NULL;
  GtkWidget *tmp_image1;
  GtkWidget *tmp_image2;
  GtkWidget *separatortoolitem2;
  GtkWidget *toolbutton5;
  GtkWidget *toolbutton6;
  GtkWidget *separatortoolitem3;
  GtkWidget *toolbutton7;
  GtkWidget *scrolledwindow1;
  GtkWidget *viewport1;
  GtkAccelGroup *accel_group;

  accel_group = gtk_accel_group_new ();

#ifndef HILDON
  gtk_window_set_title (GTK_WINDOW (window1), _("Knot 770 -"));
#endif

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox1);
  gtk_container_add (GTK_CONTAINER (window1), vbox1);

//  gtk_widget_show (menubar1);
#ifdef HILDON
  menubar1 = GTK_MENU(gtk_menu_new());
  hildon_window_set_menu(window1, menubar1);
#else
  menubar1 = gtk_menu_bar_new ();
  gtk_box_pack_start (GTK_BOX (vbox1), menubar1, FALSE, FALSE, 0);
#endif

#ifdef HILDON
  menuitem1 = gtk_menu_item_new_with_label (_("Knot"));
  new1 = gtk_image_menu_item_new_with_label (_("New"));
  open1 = gtk_image_menu_item_new_with_label (_("Open"));
  save1 = gtk_image_menu_item_new_with_label (_("Save"));
  save_as1 = gtk_image_menu_item_new_with_label (_("Save as"));
  menuitem3 = gtk_menu_item_new_with_label (_("View"));
  zoom_in1 = gtk_menu_item_new_with_label (_("Zoom in"));
  zoom_out1 = gtk_menu_item_new_with_label (_("Zoom out"));
  fullscreen = gtk_check_menu_item_new_with_label (_("Fullscreen"));
  toolbar1 = gtk_check_menu_item_new_with_label (_("Toolbar"));
#else
  menuitem1 = gtk_menu_item_new_with_mnemonic (_("_File"));
  new1 = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
  open1 = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
  save1 = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
  save_as1 = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
  menuitem3 = gtk_menu_item_new_with_mnemonic (_("_View"));
  zoom_in1 = gtk_image_menu_item_new_from_stock ("gtk-zoom-in", accel_group);
  zoom_out1 = gtk_image_menu_item_new_from_stock ("gtk-zoom-out", accel_group);
  fullscreen = gtk_check_menu_item_new_with_mnemonic (_("_Fullscreen"));
  toolbar1 = gtk_check_menu_item_new_with_mnemonic (_("_Toolbar"));
#endif

  gtk_widget_show (menuitem1);
  gtk_container_add (GTK_CONTAINER (menubar1), menuitem1);

  menuitem1_menu = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem1), menuitem1_menu);

  gtk_widget_show (new1);
  gtk_container_add (GTK_CONTAINER (menuitem1_menu), new1);

  gtk_widget_show (open1);
  gtk_container_add (GTK_CONTAINER (menuitem1_menu), open1);

  gtk_widget_show (save1);
  gtk_container_add (GTK_CONTAINER (menuitem1_menu), save1);

  gtk_widget_show (save_as1);
  gtk_container_add (GTK_CONTAINER (menuitem1_menu), save_as1);

  separatormenuitem1 = gtk_separator_menu_item_new ();

  gtk_widget_show (menuitem3);
  gtk_container_add (GTK_CONTAINER (menubar1), menuitem3);

  menuitem3_menu = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem3), menuitem3_menu);

#ifdef HILDON
  knots1 = gtk_radio_menu_item_new_with_label (knots1_group, _("Knots"));
#else
  knots1 = gtk_radio_menu_item_new_with_mnemonic (knots1_group, _("_Knots"));
#endif
  knots1_group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (knots1));
  gtk_widget_show (knots1);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), knots1);
  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (knots1), TRUE);

#ifdef HILDON
  splits1 = gtk_radio_menu_item_new_with_label (knots1_group, _("Splits"));
#else
  splits1 = gtk_radio_menu_item_new_with_mnemonic (knots1_group, _("_Splits"));
#endif
  knots1_group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (splits1));
  gtk_widget_show (splits1);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), splits1);

  separator1 = gtk_separator_menu_item_new ();
  gtk_widget_show (separator1);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), separator1);
  gtk_widget_set_sensitive (separator1, FALSE);

  gtk_widget_show (zoom_in1);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), zoom_in1);

  gtk_widget_show (zoom_out1);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), zoom_out1);

  gtk_widget_show (fullscreen);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), fullscreen);

  separator2 = gtk_separator_menu_item_new ();
  gtk_widget_show (separator2);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), separator2);
  gtk_widget_set_sensitive (separator2, FALSE);

  gtk_widget_show (toolbar1);
  gtk_container_add (GTK_CONTAINER (menuitem3_menu), toolbar1);
  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (toolbar1), TRUE);

  menuitem4 = gtk_menu_item_new_with_mnemonic (_("_Help"));

  menuitem4_menu = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menuitem4_menu);


#ifdef HILDON
  quit1 = gtk_menu_item_new_with_label("Close");
  help_on_knot1 = gtk_menu_item_new_with_label("Help");
  gtk_container_add (GTK_CONTAINER (menubar1), help_on_knot1);
  gtk_container_add (GTK_CONTAINER (menubar1), quit1);
#else
  gtk_widget_show (separatormenuitem1);
  gtk_container_add (GTK_CONTAINER (menuitem1_menu), separatormenuitem1);
  gtk_widget_set_sensitive (separatormenuitem1, FALSE);
  about1 = gtk_image_menu_item_new_from_stock("gtk-about", accel_group);
  gtk_widget_show (about1);
  quit1 = gtk_image_menu_item_new_from_stock("gtk-quit", accel_group);
  gtk_container_add (GTK_CONTAINER (menuitem1_menu), quit1);
  gtk_widget_show (menuitem4);
  gtk_container_add (GTK_CONTAINER (menubar1), menuitem4);
  help_on_knot1 = gtk_image_menu_item_new_from_stock("gtk-help", accel_group);
  gtk_container_add (GTK_CONTAINER (menuitem4_menu), help_on_knot1);
  gtk_container_add (GTK_CONTAINER (menuitem4_menu), about1);
#endif

  gtk_widget_show (help_on_knot1);
  gtk_widget_show (quit1);

  toolbar2 = gtk_toolbar_new ();
  gtk_widget_show (toolbar2);
#ifdef HILDON
  hildon_window_add_toolbar(window1, GTK_TOOLBAR(toolbar2));
  gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar2), GTK_ORIENTATION_HORIZONTAL);
  gtk_toolbar_set_style(GTK_TOOLBAR(toolbar2), GTK_TOOLBAR_ICONS);
#else
  gtk_box_pack_start (GTK_BOX (vbox1), toolbar2, FALSE, FALSE, 0);
  gtk_toolbar_set_style (GTK_TOOLBAR (toolbar2), GTK_TOOLBAR_BOTH);
  tmp_toolbar_icon_size = gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar2));
#endif
  
  toolbutton1 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-new");
  gtk_widget_show (toolbutton1);
  gtk_container_add (GTK_CONTAINER (toolbar2), toolbutton1);

  toolbutton2 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-open");
  gtk_widget_show (toolbutton2);
  gtk_container_add (GTK_CONTAINER (toolbar2), toolbutton2);

  toolbutton3 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-save");
  gtk_widget_show (toolbutton3);
  gtk_container_add (GTK_CONTAINER (toolbar2), toolbutton3);

  toolbutton4 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-save-as");
  gtk_widget_show (toolbutton4);
  gtk_container_add (GTK_CONTAINER (toolbar2), toolbutton4);

  separatortoolitem1 = (GtkWidget*) gtk_separator_tool_item_new ();
  gtk_widget_show (separatortoolitem1);
  gtk_container_add (GTK_CONTAINER (toolbar2), separatortoolitem1);

  tmp_image1 = gtk_image_new_from_file (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps/knot_tb.png");
  radiotoolbutton1 = (GtkWidget*) gtk_radio_tool_button_new (NULL);
  gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON(radiotoolbutton1), tmp_image1);
#ifndef HILDON
  gtk_tool_button_set_label(GTK_TOOL_BUTTON(radiotoolbutton1), "Knots");
#endif
  gtk_widget_show (radiotoolbutton1);
  gtk_container_add (GTK_CONTAINER (toolbar2), radiotoolbutton1);
  gtk_radio_tool_button_set_group (GTK_RADIO_TOOL_BUTTON (radiotoolbutton1), radiotoolbutton1_group);
  radiotoolbutton1_group = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (radiotoolbutton1));

  tmp_image2 = gtk_image_new_from_file (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps/split_tb.png");
  radiotoolbutton2 = (GtkWidget*) gtk_radio_tool_button_new (NULL);
  gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON(radiotoolbutton2), tmp_image2);
#ifndef HILDON
  gtk_tool_button_set_label(GTK_TOOL_BUTTON(radiotoolbutton2), "Splits");
#endif
  gtk_widget_show (radiotoolbutton2);
  gtk_container_add (GTK_CONTAINER (toolbar2), radiotoolbutton2);
  gtk_radio_tool_button_set_group (GTK_RADIO_TOOL_BUTTON (radiotoolbutton2), radiotoolbutton1_group);
  radiotoolbutton1_group = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (radiotoolbutton2));

  separatortoolitem2 = (GtkWidget*) gtk_separator_tool_item_new ();
  gtk_widget_show (separatortoolitem2);
  gtk_container_add (GTK_CONTAINER (toolbar2), separatortoolitem2);

  toolbutton5 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-zoom-in");
  gtk_widget_show (toolbutton5);
  gtk_container_add (GTK_CONTAINER (toolbar2), toolbutton5);

  toolbutton6 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-zoom-out");
  gtk_widget_show (toolbutton6);
  gtk_container_add (GTK_CONTAINER (toolbar2), toolbutton6);

  separatortoolitem3 = (GtkWidget*) gtk_separator_tool_item_new ();
  gtk_widget_show (separatortoolitem3);
  gtk_container_add (GTK_CONTAINER (toolbar2), separatortoolitem3);

  toolbutton7 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-quit");
  gtk_widget_show (toolbutton7);
  gtk_container_add (GTK_CONTAINER (toolbar2), toolbutton7);

  scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (scrolledwindow1);
  gtk_box_pack_end (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  viewport1 = gtk_viewport_new (NULL, NULL);
  gtk_widget_show (viewport1);
  gtk_container_add (GTK_CONTAINER (scrolledwindow1), viewport1);
  gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport1), GTK_SHADOW_NONE);

  drawingArea = gtk_drawing_area_new ();
  gtk_widget_show (drawingArea);
  gtk_widget_set_size_request(drawingArea, kwidth * TILEWIDTH, kheight * TILEHEIGHT);
  gtk_container_add (GTK_CONTAINER (viewport1), drawingArea);

  g_signal_connect( G_OBJECT(window1), "delete_event", gtk_main_quit, NULL );
  g_signal_connect( G_OBJECT(window1), "key_press_event", G_CALLBACK(key_press_cb), NULL );

  g_signal_connect (G_OBJECT(drawingArea), "expose_event", G_CALLBACK(expose_event_callback), NULL);
  gtk_widget_add_events(drawingArea, GDK_BUTTON_PRESS_MASK);
  g_signal_connect (G_OBJECT(drawingArea), "button_press_event", G_CALLBACK(button_press_event_callback), NULL);

  g_signal_connect ((gpointer) new1, "activate",
                    G_CALLBACK (on_new1_activate),
                    NULL);
  g_signal_connect ((gpointer) toolbutton1, "clicked",
                    G_CALLBACK (on_new_tb),
                    NULL);
  g_signal_connect ((gpointer) open1, "activate",
                    G_CALLBACK (on_open1_activate),
                    NULL);
  g_signal_connect ((gpointer) toolbutton2, "clicked",
                    G_CALLBACK (on_open_tb),
                    NULL);
  g_signal_connect ((gpointer) save1, "activate",
                    G_CALLBACK (on_save1_activate),
                    NULL);
  g_signal_connect ((gpointer) toolbutton3, "clicked",
                    G_CALLBACK (on_save_tb),
                    NULL);
  g_signal_connect ((gpointer) save_as1, "activate",
                    G_CALLBACK (on_save_as1_activate),
                    NULL);
  g_signal_connect ((gpointer) toolbutton4, "clicked",
                    G_CALLBACK (on_save_as_tb),
                    NULL);
  g_signal_connect ((gpointer) quit1, "activate",
                    G_CALLBACK (on_quit1_activate),
                    NULL);
  g_signal_connect ((gpointer) toolbutton7, "clicked",
                    G_CALLBACK (on_quit1_activate),
                    NULL);
  g_signal_connect ((gpointer) knots1, "activate", G_CALLBACK (on_knots_menu), NULL);
  g_signal_connect ((gpointer) splits1, "activate", G_CALLBACK (on_splits_menu), NULL);

  g_signal_connect ((gpointer) radiotoolbutton1, "clicked", G_CALLBACK (on_knots_tb), NULL);
  g_signal_connect ((gpointer) radiotoolbutton2, "clicked", G_CALLBACK (on_splits_tb), NULL);

  g_signal_connect ((gpointer) zoom_in1, "activate",
                    G_CALLBACK (on_zoom_in1_activate),
                    NULL);
  g_signal_connect ((gpointer) zoom_out1, "activate",
                    G_CALLBACK (on_zoom_out1_activate),
                    NULL);
  g_signal_connect ((gpointer) fullscreen, "activate",
                    G_CALLBACK (on_fullscreen_activate),
                    NULL);
  g_signal_connect ((gpointer) toolbar1, "activate",
                    G_CALLBACK (on_toolbar_toggle),
                    NULL);
  g_signal_connect ((gpointer) help_on_knot1, "activate",
                    G_CALLBACK (on_help_on_knot1_activate),
                    NULL);
#ifndef HILDON
  g_signal_connect ((gpointer) about1, "activate",
                    G_CALLBACK (on_about1_activate),
                    NULL);
#endif

  gtk_window_add_accel_group (GTK_WINDOW (window1), accel_group);
}


GtkWidget* create_new_dialog (void)
{
  GtkWidget *new_dialog;
  GtkWidget *dialog_vbox1;
  GtkWidget *vbox1;
  GtkWidget *hbox1;
  GtkWidget *label1;
  GtkWidget *hbox2;
  GtkWidget *label2;
  GtkWidget *dialog_action_area1;
  GtkWidget *cancelbutton1;
  GtkWidget *okbutton1;

  new_dialog = gtk_dialog_new ();
  gtk_window_set_title (GTK_WINDOW (new_dialog), _("New knot"));
  gtk_window_set_position (GTK_WINDOW (new_dialog), GTK_WIN_POS_CENTER_ON_PARENT);
  gtk_window_set_modal (GTK_WINDOW (new_dialog), TRUE);
  gtk_window_set_type_hint (GTK_WINDOW (new_dialog), GDK_WINDOW_TYPE_HINT_DIALOG);

  dialog_vbox1 = GTK_DIALOG (new_dialog)->vbox;
  gtk_widget_show (dialog_vbox1);

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox1);
  gtk_box_pack_start (GTK_BOX (dialog_vbox1), vbox1, TRUE, TRUE, 0);

  hbox1 = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox1);
  gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);

  label1 = gtk_label_new (_("Width: "));
  gtk_widget_show (label1);
  gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);

  hscale1 = gtk_hscale_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 21, 1, 1, 1)));
  gtk_widget_show (hscale1);
  gtk_box_pack_start (GTK_BOX (hbox1), hscale1, TRUE, TRUE, 0);
  gtk_scale_set_digits (GTK_SCALE (hscale1), 0);

  hbox2 = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox2);
  gtk_box_pack_start (GTK_BOX (vbox1), hbox2, TRUE, TRUE, 0);

  label2 = gtk_label_new (_("Height: "));
  gtk_widget_show (label2);
  gtk_box_pack_start (GTK_BOX (hbox2), label2, FALSE, FALSE, 0);

  hscale2 = gtk_hscale_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 12, 1, 1, 1)));
  gtk_widget_show (hscale2);
  gtk_box_pack_start (GTK_BOX (hbox2), hscale2, TRUE, TRUE, 0);
  gtk_scale_set_digits (GTK_SCALE (hscale2), 0);

  dialog_action_area1 = GTK_DIALOG (new_dialog)->action_area;
  gtk_widget_show (dialog_action_area1);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);

  cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
  gtk_widget_show (cancelbutton1);
  gtk_dialog_add_action_widget (GTK_DIALOG (new_dialog), cancelbutton1, GTK_RESPONSE_CANCEL);
  GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);

  okbutton1 = gtk_button_new_from_stock ("gtk-ok");
  gtk_widget_show (okbutton1);
  gtk_dialog_add_action_widget (GTK_DIALOG (new_dialog), okbutton1, GTK_RESPONSE_OK);
  GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);

  gtk_window_set_transient_for(GTK_WINDOW(new_dialog), GTK_WINDOW(window));
  return new_dialog;
}

